<?php

namespace app\api\logic;
use app\admin\logic\BasicLogic;
use think\facade\Db;
class DulizhanLogic extends BasicLogic
{
    public function AddSKUDetails($params=[]){
        $id       = $params['id']??'';
        if(!$id)   return ['msg'=>'订单ID不能为空!'];

        $test_order_quantity = Db::connect('DULIZHAN')
            ->name('order_quantity')
            ->where('order_id',$id)
            ->select();
        $skuinfo = "";
        $skuinfo2 = "";
        $new_spu = [];
        if(!empty($test_order_quantity)){
            foreach ($test_order_quantity as $kq=>$vq) {
                $insert = [];
                $insert['order_id'] = $vq['order_id'];
                $insert['sku'] = $vq['sku'];
                $productsku = $this->productsku($vq);
                $insert['num'] = $productsku['pieces_new'];;
                $insert['spu'] = $productsku['brand'];
                $new_spu[$productsku['brand']] = 1;
                if(!empty($skuinfo)){
                    $skuinfo = $skuinfo.",".$vq['sku']. "*" .$vq['quantity'];
                }else{
                    $skuinfo = $vq['sku']. "*" .$vq['quantity'];
                }
                if(!empty($skuinfo2)){
                    $skuinfo2 = $skuinfo2.",".$insert['spu']. "*" .$insert['num'];
                }else{
                    $skuinfo2 = $insert['spu']. "*" .$insert['num'];
                }
            }
        }
        
        $update_info = [];
        $update_info['skuinfo'] = $skuinfo;
        $update_info['skuinfo2'] = $skuinfo2;
        $update_info['skuinfo_time'] = time();

        if(!empty($new_spu)){
            $update_info['two_kf_id'] = count($new_spu);
        }else{
            $update_info['two_kf_id'] = 1;
        }
        
        if(!empty($update_info)){
            Db::connect('DULIZHAN')
            ->name('order')
            ->where("id",$id)
            ->update($update_info);
        }
        
        return  true;
    }


    /**
    * 方法详情
    * @param array params
    */
    public function productsku($v)
    {
        $productsku['brand'] = "";
        $productsku['pieces_new'] = 0;
        $count = 0;
        if (!empty($v['sku']) && !empty($v['quantity'])) {
            //拆分规则
            $pos1 = strpos($v['sku'], '+');
            if ($pos1 !== false) {
                $pos = strpos($v['sku'], '-');
                if ($pos !== false) {
                    $sku = explode('-', $v['sku']);
                    $productsku['brand'] = $sku[0];
                    $combination_sku = explode('+', $sku[1]);
                    $count = count($combination_sku);
                    if(isset($sku[3]) && !empty($sku[3])){
                        $count = (int)number_format((int)$sku[3] * (int)$count, 0, '.', '');
                    }
                }else{
                    $count = 2;
                }
            } else {
                $pos = strpos($v['sku'], '-');
                if ($pos !== false) {
                    $sku = explode('-', $v['sku']);
                    $productsku['brand'] = $sku[0];
                    if (isset($sku[3])) {
                        if (is_numeric($sku[3])) {
                            $count = $sku[3];
                        }
                    } else {
                        $count = 1;
                    }
                }else{
                    $count = 2;
                }
            }
            $count = (int)number_format((int)$v['quantity'] * (int)$count, 0, '.', '');
        }
        $productsku['pieces_new'] = $count;
        return $productsku; 
    }

    //整理订单是否被结算
    public function HasTheOrderBeenSettled($params=[]){
        $id       = $params['order_id']??'';
        if(!$id)   return ['msg'=>'订单ID不能为空!'];

        $test_order_quantity = Db::connect('DULIZHAN')
            ->name('jiesuaninfo')
            ->where('order_id',$id)
            ->select();

        $update_info = [];
        if(!empty($test_order_quantity)){
            foreach ($test_order_quantity as $kq=>$vq) {
                $key = "info".$kq;
                $update_info[$key] = $vq['money'];
            }
            $update_info["status"] = "完成";
        }else{
            $update_info["status"] = "未找到";
        }
        
        if(!empty($update_info)){
            Db::connect('DULIZHAN')
                ->name('jiesuan')
                ->where("order_id",$id)
                ->update($update_info);
        }
        
        return  true;
    }

}